const Router = require('koa-router')
const { sequelize } = require('../database/db')
const { Tutorial } = require('../database/models/tutorial')
const check = require('../pem/check')

const router = new Router()

router.post('/', async(ctx) => {
    const tutorialList = await Tutorial.findAll({
        order: [
            ['sort', 'desc']
        ]
    })
    ctx.body = {
        code: 101,
        data: tutorialList,
        msg: ''
    }
})

router.post('/add', async(ctx) => {
    const { title, content, content_text, sort } = ctx.request.body
    if (title && content && content_text) {
        const verify = await check(ctx.request.headers.authorization)
        if (verify) {
            try {
                await Tutorial.create({
                    title, content, content_text,
                    sort: sort || 0
                })
                ctx.body = {
                    code: 101,
                    data: {
                        title, content, content_text, sort: sort || 0
                    },
                    msg: '成功'
                }
            } catch (err) {
                ctx.body = {
                    code: 102,
                    data: null,
                    msg: ''
                }
            }
        } else {
            ctx.body = {
                code: 201,
                data: null,
                msg: 'token过期'
            }
        }
    } else {
        ctx.body = {
            code: 102,
            data: null,
            msg: ''
        }
    }
})

router.post('/update', async(ctx) => {
    const { title, content, content_text, sort, id } = ctx.request.body
    if (id) {
        const verify = await check(ctx.request.headers.authorization)
        if (verify) {
            try {
                await Tutorial.update({
                    title, content, content_text,
                    sort: sort || 0
                }, { 'where': { id }})
                ctx.body = {
                    code: 101,
                    data: {
                        title, content, content_text, sort: sort || 0, id
                    },
                    msg: '成功'
                }
            } catch (err) {
                ctx.body = {
                    code: 102,
                    data: null,
                    msg: ''
                }
            }
        } else {
            ctx.body = {
                code: 201,
                data: null,
                msg: 'token过期'
            }
        }
    } else {
        ctx.body = {
            code: 102,
            data: null,
            msg: ''
        }
    }
})

router.post('/batchDel', async(ctx) => {
    const { id } = ctx.request.body
    if (id) {
        const verify = await check(ctx.request.headers.authorization)
        if (verify) {
            const data = await sequelize.query(
                `DELETE FROM Tutorials WHERE id IN (${id.join(',')})`
            )
            if (data) {
                ctx.body = {
                    code: 101,
                    data: id,
                    msg: '成功'
                }
            } else {
                ctx.body = {
                    code: 102,
                    data: null,
                    msg: ''
                }
            }
        } else {
            ctx.body = {
                code: 201,
                data: null,
                msg: 'token过期'
            }
        }
    } else {
        ctx.body = {
            code: 102,
            data: null,
            msg: ''
        }
    }
})

module.exports = router
